package com.dh.spring.client.file.common;

import com.dh.spring.client.http.*;
import com.dh.spring.common.BasicConstant;
import com.dh.spring.common.HttpResponse;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;

/**
 * @Auther: YSFY
 * @Date: 2020-09-10 12:05
 * @Pacage_name: com.dh.spring.client.file.common
 * @Project_Name: arthas-client
 * @Description: 文件校验线程,只负责文件上传或下载与文件服务端的连接过程，连接成功后注册文件写事件,发送文件信息进行校验，并返回当前过程结果
 */

@Slf4j
@SuppressWarnings("all")
public final class FileCheckRunnable implements Callable<FileCheckRunnable.FileCheckResult> {
    private String fileName;
    private RequestData requestData;
    private FileCheckResult fileCheckResult;

    public FileCheckRunnable(String fileName, RequestData requestData) {
        this.fileName = fileName;
        this.requestData = requestData;
        this.fileCheckResult = new FileCheckResult();
        this.fileCheckResult.setFileName(fileName);
    }

    @Override
    public FileCheckResult call() throws Exception {
        if(this.requestData.getExec().equals(BasicConstant.UPLOAD))
            return HttpUploadHandler.executeFileUpload(this.fileName, this.requestData, this.fileCheckResult);
        if(this.requestData.getExec().equals(BasicConstant.DOWNLOAD))
            return HttpDownloadHandler.executeFileDownload(this.fileName, this.requestData, this.fileCheckResult);
        if(this.requestData.getExec().equals(BasicConstant.LINUX_COMMAND)) {
            FileExecStatus fileExecStatus = new FileExecStatus();
            fileExecStatus.setFileName(fileCheckResult.getFileName());
            fileExecStatus.setFileExec(requestData.getExec());
            fileExecStatus.setStatus(1);
            return LinuxCommandHandler.executeLinuxCommandDownload(this.requestData, this.fileCheckResult, fileExecStatus);
        }

        return this.fileCheckResult;
    }

    @Data
    public final class FileCheckResult {
        private String fileName;
        private boolean isHandler;
        private String result;
    }
}
